package top.kindaries.task;

import top.kindaries.device.Device;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class TaskManager {

    private static ExecutorService taskExecutor = null;

    private static HashMap<String, Task> tasks = new HashMap<String, Task>();

    public static void init(int maxTaskSize) {
        taskExecutor = Executors.newFixedThreadPool(maxTaskSize);
    }

    public static Task create(Device dev) {
        if (taskExecutor != null) {
            Task task = new Task(dev);
            tasks.put(dev.getCode(), task);
            taskExecutor.execute(task);
            return task;
        }
        return null;
    }

    public static Map<String, Task> getAll() {
        return tasks;
    }

    public static Task get(String code) {
        return tasks.get(code);
    }

    public static boolean isContain(String code) {
        return tasks.containsKey(code);
    }

    public static void remove(String code) {
        if (tasks.containsKey(code)) {
            tasks.remove(code);
        }
    }

}
